// JavaScript Document
PlanetBirthday = function () {
	{
		var pb = this
		pb.DAYS = 365.256898326;
		pb.DAY = 86300000;
		pb.YEAR = 31556925251;//31558196015;//31557600000;//
		
		pb.PLANETS = {
			mercury:{label:"Mercury", yearRatio:0.241},
			venus:{label:"Venus", yearRatio:0.615},
			earth:{label:"Earth", yearRatio:1},
			mars:{label:"Mars", yearRatio:1.881},
			jupiter:{label:"Jupiter", yearRatio:11.862},
			saturn:{label:"Saturn", yearRatio:29.457},
			uranus:{label:"Uranus", yearRatio:84.011},
			neptune:{label:"Neptune", yearRatio:163.79},
			pluto:{label:"Pluto" , yearRatio:247.68},
			tatooine:{label:"Tatooine", yearRatio:0.7976 }, 
			lv426:{label:"LV426 (acheron)", yearRatio:1.184 }
		};
		pb.PLANETS_ARRAY = [
			pb.PLANETS.mercury,
			pb.PLANETS.venus,
			pb.PLANETS.earth,
			pb.PLANETS.mars,
			pb.PLANETS.jupiter,
			pb.PLANETS.saturn,
			pb.PLANETS.uranus,
			pb.PLANETS.neptune,
			pb.PLANETS.pluto
		];
		pb.FICTIONAL_PLANETS = {
		};
		pb.MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] ;
		pb.DAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] ;

		pb.getDateSuffix = function(date){
			var suffix
			var dateString = date;
			dateString = dateString<10 ? "0"+dateString : dateString.toString();;
			var switchDate = dateString.substr(0, 1) == "1" ? 0 : dateString.substr(1, dateString.length-1);
			switch(switchDate){
				case "1":
				suffix = date+"st";
				break;
				case "2":
				suffix = date+"nd";
				break;
				case "3":
				suffix = date+"rd";
				break;
				default :
				suffix = date+"th";
				break;
			}
			return suffix;
			
		}
		
		pb.dateToMessage = function (date, planet) {
			var str
			var d = new Date()
			d.setHours(0, 0, 0, 0);
			if(d.getTime() == date.getTime()){
				str = "Happy Birthday!"
			} else {
				str = pb.DAYS[date.getDay()]+" the "+pb.getDateSuffix(date.getDate())+" of "+pb.MONTHS[date.getMonth()]+" "+date.getFullYear() ;
			}
			return str;
		}
		
		pb.getBirthday = function (day, month, year, planetYearRatio){
			var date = new Date(year, month-1, day, 0, 0, 0, 0);
			var today = new Date();
			today.setHours(0,0,0,0)
			var earthYears = today.getTime() - date.getTime();
			var age = (earthYears/(pb.YEAR*planetYearRatio)+1)>>0;
			var earthAge = (earthYears/pb.YEAR)>>0;
			var planetAge = (earthYears/(pb.YEAR*planetYearRatio))>>0;
			var birthdays = [];
			if(planetYearRatio == 1){
				today.setMonth(month-1, day);
				birthdays[0] = today;
			} else {
				birthdays[0] = new Date(date.getTime()+((pb.YEAR*planetYearRatio)*(planetAge+1)));
			}
			var i = 1;
			var currentBirthday
			var o = {};
			o.earthAge = earthAge;
			o.planetYearLength = pb.YEAR*planetYearRatio
			o.planetAge = planetAge;
			o.birthdays = birthdays; 
			currentBirthday = birthdays[0];
			while(currentBirthday.getTime() < (today.getTime()+pb.YEAR)){
				
				birthdays[i] = new Date(birthdays[i-1].getTime()+(pb.YEAR*planetYearRatio));
				currentBirthday = birthdays[i];
				++i
			}

			o.nextBirthday = birthdays[0];
			return o;
		}
		if(!window.$PlanetBirthday){window.$PlanetBirthday=pb};
	}
}